package main;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import gate.Annotation;
import gate.AnnotationSet;
import gate.Corpus;
import gate.Factory;
import gate.FeatureMap;
import gate.Gate;
import gate.GateConstants;
import gate.ProcessingResource;
import gate.annotation.AnnotationSetImpl;
import gate.corpora.DocumentContentImpl;
import gate.corpora.DocumentImpl;
import gate.creole.ANNIEConstants;
import gate.creole.ResourceInstantiationException;
import gate.creole.SerialAnalyserController;
import gate.creole.annic.apache.lucene.document.Document;
import gate.util.GateException;
import gate.util.Out;

public class Test {

	/**
	 * @param args
	 * @throws GateException 
	 * @throws MalformedURLException 
	 */
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws GateException, MalformedURLException {
		
		Gate.init();
		gate.Corpus corpus= (Corpus) Factory.createResource("gate.corpora.CorpusImpl");
		
		//A) You need to register the plugin before you load it.
		Gate.getCreoleRegister().registerDirectories(new File(Gate.getPluginsHome(), ANNIEConstants.PLUGIN_DIR).toURI().toURL());
		System.out.println(new File(Gate.getPluginsHome(), ANNIEConstants.PLUGIN_DIR).toURI().toURL());
		
     //   Gate.getCreoleRegister().registerDirectories(new URL("file:/Applications/GATE_Developer_7.1/plugins/Tagger_Numbers/"));
        
        
        DocumentImpl doc = new DocumentImpl();
       
        //B) The string to be annotated.
        String str = "Height is 60 in. Weight is 150 lbs pulse rate 90 Pulse rate 90. Europeana Content Development Strategy. ESMAE, Bruno Pereira. Emanuele Bellini, Raffaella Santucci, Lotte Belice Baltussen, Paolo Nesi, Perla Innocenti ";
        DocumentContentImpl impl = new DocumentContentImpl(str);
        doc.setContent(impl);
        corpus.add(doc); //va messo qui?
        
        //C) Loading processing resources. refer http://gate.ac.uk/gate/doc/plugins.html for what class the plugin belongs to
        // http://gate.ac.uk/gate/doc/plugins.html
      //  ProcessingResource token = (ProcessingResource) Factory.createResource("gate.creole.tokeniser.DefaultTokeniser", Factory.newFeatureMap());
      // ProcessingResource sspliter = (ProcessingResource) Factory.createResource("gate.creole.splitter.SentenceSplitter", Factory.newFeatureMap());
      //  ProcessingResource number = (ProcessingResource) Factory.createResource("gate.creole.numbers.NumbersTagger", Factory.newFeatureMap());
       
       
        
        /* pipeline is an application that needs to be created to use resources loaded above.
        Reasources must be added in a particular order eg. below the 'number' resource requires the document to be tokenised. */
        
        //D) Create and Initialize pipeline
        SerialAnalyserController pipeline = (SerialAnalyserController) Factory.createResource("gate.creole.SerialAnalyserController", Factory.newFeatureMap(), Factory.newFeatureMap(), "ANNIE_" + Gate.genSym());
      
        //E) Carica il corpus nel pipeline
         pipeline.setCorpus(corpus);
        
        //F) Carica i processing Resourcer nel pipeline
       // pipeline.add(token);
       // pipeline.add(sspliter);
      //  pipeline.add(number);
      
        //C1,F1) In alternativa ai passi C,F, si caricano tutti i ProcessingResource disponibili per annie
        
        for(int i = 0; i < ANNIEConstants.PR_NAMES.length; i++) {
        ProcessingResource pr = (ProcessingResource)
        Factory.createResource(ANNIEConstants.PR_NAMES[i], Factory.newFeatureMap() );
        pipeline.add(pr);
        }
        
        pipeline.execute();
        
        
 /*      //Extract info from an annotated document.
        AnnotationSetImpl ann = (AnnotationSetImpl) doc.getAnnotations();
        Iterator<Annotation>i = ann.get().iterator();
        String corpusString= corpus.get(0).toString();
        while(i.hasNext()){
    	    Annotation annotation = i.next();
    	    long start = annotation.getStartNode().getOffset();
    	    long end =  annotation.getEndNode().getOffset();
    	    System.out.print(corpusString.substring((int)start, (int)end)+" ");
        }*/
        
        ////Boh
        Iterator iter = corpus.iterator();
        while(iter.hasNext()){
        	doc = (DocumentImpl) iter.next();
        	AnnotationSet defaultAnnotSet = doc.getAnnotations();
        	Set annotTypesRequired = new HashSet();
            annotTypesRequired.add("Person");
        	Set<Annotation> peopleAndPlaces =
                new HashSet<Annotation>(defaultAnnotSet.get(annotTypesRequired));
        	
        	
        	Iterator it = peopleAndPlaces.iterator();
        	long endPosition;
            long startPosition;
        	 while(it.hasNext()) {
        		 Annotation currAnnot= (Annotation) it.next();
        		 startPosition =
                     currAnnot.getStartNode().getOffset().longValue();
                   endPosition = currAnnot.getEndNode().getOffset().longValue();
                   if(endPosition != -1 && startPosition != -1) {
                 	/* Out.prln(((String)
                 	        doc.getFeatures().get(GateConstants.ORIGINAL_DOCUMENT_CONTENT_FEATURE_NAME)).substring((int)startPosition, (int)endPosition));
                   */
                	   Out.prln(str.substring((int)startPosition, (int)endPosition));
                     
                   }
        	 }
        }
        
       

	}

}
